<?php
/**
 * Medical Center Patients List
 *
 * @author Xavier VILAIN
 * @package
 * @license
 */

use Fox\Dao\SqlFragment;
use Nawa\MedicalStudy\MedicalCenter\Patient\Status;
use Fox\Behaviors;
use Nawa\MedicalStudy\MedicalCenter;
use Fox\Html\Block;
use Fox\Grid;
use Fox\Generator;
use Fox\Html\Button;

$xview = Xview::instance();
$msid = $xview->val('msid');
$mcid = $xview->val('mcid');
$monitoringid = $xview->val('monitoringid');
$inlineid = $msid.':'.$mcid;
// Load Documents
$medicalCenterPartner = \Fox\Dao::load(MedicalCenter::className(),$inlineid);

if(!empty($monitoringid)) {
	$monitoring = $medicalCenterPartner->findMonitorings($medicalCenterPartner->getInlineId().':'.$monitoringid);
	$patients = $monitoring->patients();
} else {
	$patients = $medicalCenterPartner->patients();
}
$grid = new Grid('patients');
$grid->fieldcat(array('patientid',array('patient_ref','reference'),'initials','birth_date','gender',
				'selection_date','inclusion_date','status','iedit','idele'));
$grid->fieldcat()->field('patientid')->hidden();
$grid->fieldcat()->field('initials')->noOutput();
$grid->fieldcat()->field('gender')->noOutput();
$grid->fieldcat()->field('inclusion_date')->noOutput();
$genDoc = Generator::url('medicalstudies','medicalcenters', 'patients',
array('msid' => $msid,'mcid' => $mcid));
$grid->source($genDoc);
$grid->fieldcat()->switchToObjectMode();
$grid->initialize($patients);

// Status
$statusesLabels = array();
$finder = new Status();
$statuses = $finder->find(new SqlFragment('disable = ?',array(0)));
foreach($statuses as $status) {
	$statusesLabels[$status->status] = $status->label;
}
$grid->fieldcat()->field('status')->setGenerator(
	Generator::useFunction(function($data) use ($statusesLabels) {
		if(isset($statusesLabels[$data->status])) {
			return $statusesLabels[$data->status];
		}
		return $data->status;
	})
);
// Delete Document action
$grid->fieldcat()->field('idele')
	->setGenerator(Generator::useValue('?'))
	->renderer()->behaviors(
	Behaviors::action(
		Generator::url('medicalstudies','patients','delete',
						array(	'msid' => $msid,
								'mcid' => $mcid,
								'patientid' => Generator::useAttribute('patientid'))
					),
		true,
		null,
		null,
		true
));
if(!empty($monitoringid)) {
	$grid->fieldcat()->field('idele')->noOutput();
}
// Update Document action
$grid->fieldcat()->field('iedit')
	->setGenerator(Generator::useValue('?'))
	->renderer()->behaviors(
	Behaviors::action(
		Generator::url('medicalstudies','patients','update',
						array(	'msid' => $msid,
								'mcid' => $mcid,
								'patientid' => Generator::useAttribute('patientid'))
					),
		true,
		null,
		null,
		true
));
// Action on click -> redirect to patient monitoring page
$grid->onClick(Generator::location(
		Generator::url('medicalstudies','patient',null,array(
			'msid' => $msid,
			'mcid' => $mcid,
			'patientid' => Generator::useAttribute('patientid')
		))
));
$grid->footer()
	->add(new Button(array('add','addpatient')))
	->behaviors(Behaviors::action(Generator::url('medicalstudies','patients','add',array('msid' => $msid,'mcid' => $mcid)),
	true,
	null));
if($xview->ajax())
{
	echo $grid->display();
}
else
{
	$block = new Block('bPatients',Msg(Nawa\Messages::BUNDLE,Nawa\Messages::LABEL_PATIENTS));
	$block->add($grid);
	return $block;
}